package com.ningxia.water.controller;

import com.ningxia.water.enums.WarnCodeEnum;
import com.ningxia.water.enums.WarnStatusEnum;
import com.ningxia.water.enums.WellStatusEnum;
import com.ningxia.water.reuslt.Result;
import com.ningxia.water.reuslt.ResultResponse;
import com.ningxia.water.service.ITsWellInfoService;
import com.ningxia.water.service.ITsWellWarnService;
import com.ningxia.water.vo.out.WarnInfoVo;
import com.ningxia.water.vo.out.WarnCountsVo;
import com.ningxia.water.vo.in.WarnUpdateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cqm
 * <p>
 * 报警信息
 */
@RestController
@RequestMapping("/warn")
@Api(tags = "业务管理-报警管理Api")
public class WarnInfoController {

    @Autowired
    private ITsWellInfoService wellInfoService;

    @Autowired
    private ITsWellWarnService warnService;


    @GetMapping("/getwarninfos")
    @ApiOperation(value = "获取全部报警记录")
    public Result getWarnInfos() {
        List<WarnInfoVo> lists = warnService.getWarnInfos();
        return ResultResponse.getSuccessResult(lists);
    }

    /**
     * 多条件报警信息查询
     *
     * @return
     */
    @GetMapping("/findwarninfo")
    @ApiOperation(value = "条件查询报警信息")
    public Result findWarnInfo(@RequestParam(value = "applicantName", required = false) String applicantName,
                               @RequestParam(value = "wellName", required = false) String wellName,
                               @RequestParam(value = "status", required = false) Integer status,
                               @RequestParam(value = "startTime", required = false) String startTime,
                               @RequestParam(value = "endTime", required = false) String endTime) {

        List<WarnInfoVo> warnInfos = warnService.findWarnByConditions(applicantName, wellName, status, startTime, endTime);
        return ResultResponse.getSuccessResult(warnInfos);
    }

    /**
     * 报警信息状态更改
     */
    @PutMapping("/updatewarn")
    @ApiOperation(value = "更改报警信息状态")
    public Result updateWarnStatus(@RequestBody WarnUpdateVo warnUpdate) {
        //修改报警记录是否已处理
        Integer row1 = warnService.updateByWarnId(warnUpdate.getId(), warnUpdate.getStatus());

        //根据机井id查询 此机井是否还有未处理的记录
        Integer count = warnService.getWarnCountsByWellId(warnUpdate.getWellId());

        //如果此机井下还有未处理的报警记录，那么机井状态就不改变。
        //row1，row2：表示是否修改成功
        Integer row2;
        if (warnUpdate.getStatus() == WarnStatusEnum.PROCESSED.getCode() && count == 0) {
            //如果 将未处理 报警信息修改为 已处理状态，需要将机井状态改为正常
            row2 = wellInfoService.updateStatus(warnUpdate.getWellId(), WellStatusEnum.RUNNING.getCode());
        } else {
            //如果 将已处理信息修改为 未处理状态，需要将机井状态改为报警
            row2 = wellInfoService.updateStatus(warnUpdate.getWellId(), WellStatusEnum.MALFUNCTION.getCode());
        }

        if (row1 > 0 && row2 > 0) {
            return ResultResponse.getSuccessResult();
        } else {
            return ResultResponse.getSuccessResult("更新失败");
        }
    }

    // 未处理的报警记录中 ，不同报警类型数量
    @GetMapping("/warncount")
    @ApiOperation(value = "获取不同报警类型数量")
    public Result getWarnStatus() {
        //获取未处理报警记录数量
        Integer warnCounts = warnService.getWarnCountsByStatus(WarnStatusEnum.UNTREATED.getCode());
        //未处理的水量超额报警数量
        Integer excessCounts = warnService.findByWarnCode(WarnCodeEnum.VOLUME_EXPRESS.getCode());
        //未处理的机井故障报警数量
        Integer wellErrorCounts = warnService.findByWarnCode(WarnCodeEnum.WELL_MALFUNCTION.getCode());
        //未处理的流量异常报警数量
        Integer flowAbnormalCounts = warnService.findByWarnCode(WarnCodeEnum.VOLUME_ABNORMAL.getCode());
        //未处理的电池电量不足报警数量
        Integer batteryNotEnoughCounts = warnService.findByWarnCode(WarnCodeEnum.WELL_MALFUNCTION.getCode());

        WarnCountsVo result = new WarnCountsVo(warnCounts, excessCounts, wellErrorCounts, flowAbnormalCounts, batteryNotEnoughCounts);

        return ResultResponse.getSuccessResult(result);
    }

}
